﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using admin.cyt369.com.Models;
using Admin.CYT369.Com.WebBase;

namespace admin.cyt369.com.Controllers
{
    public partial class DepartmentsController
    {
        #region GetDepartmentFullTree
        [HttpGet]
        public IActionResult GetDepartmentFullTree()
        {
            var not_id = Request.Query.GetInteger("NOT_ID");
            bool? enable = this.Request.Query.GetBoolean("Enable");

            var organizations = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetOrganizations().ToArray();
            var departments = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetDepartments(enable:enable).ToArray();
            var list = _MakeDepartmentsTree(organizations, departments, 0, not_id);

            return DefaultJson(list);
        }

        private List<admin.cyt369.com.Models.Orgs.OrgDepartmentTree> _MakeDepartmentsTree(IEnumerable<Admin.CYT369.Com.DAL.Domains.Organization> organizations,
                                                                                                    IEnumerable<Admin.CYT369.Com.DAL.Domains.Department> departments,
                                                                                                    int parentOrgId,
                                                                                                    int? not_id)
        {
            var list = new List<admin.cyt369.com.Models.Orgs.OrgDepartmentTree>();

            foreach (var org in organizations.Where(c=>c.ParentID == parentOrgId))
            {
                var item = new admin.cyt369.com.Models.Orgs.OrgDepartmentTree(org);

                item.children = _MakeDepartmentsTree(departments, 0, org.Id, not_id);
                item.children.AddRange(_MakeDepartmentsTree(organizations, departments, org.Id, not_id));
                item.state = "open";

                list.Add(item);
            }

            return list;
        }

        private List<admin.cyt369.com.Models.Orgs.OrgDepartmentTree> _MakeDepartmentsTree(IEnumerable<Admin.CYT369.Com.DAL.Domains.Department> departments, int parentDeptId, int? orgId = null, int? not_id = null) {
            var list = new List<admin.cyt369.com.Models.Orgs.OrgDepartmentTree>();
            var depts = departments.Where(c => c.ParentID == parentDeptId);
            if (orgId.HasValue)
            {
                depts = depts.Where(c=>c.OrgID == orgId.Value);
            }

            foreach (var dept in depts)
            {
                if (not_id.HasValue)
                {
                    if (dept.Id == not_id.Value)
                    {
                        continue;
                    }
                }
                var item = new admin.cyt369.com.Models.Orgs.OrgDepartmentTree(dept);
                item.children = _MakeDepartmentsTree(departments, dept.Id, orgId, not_id);
                item.state = "open";

                list.Add(item);
            }

            return list;
        }
        #endregion

        [HttpGet]
        public IActionResult GetDepartments()
        {

            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;


            var orgId = Request.Query.GetInteger("OrganizationID");
            var not_id = Request.Query.GetInteger("NOT_ID");
            var parentId = Request.Query.GetInteger("ParentID", 0);
            var departmentName = Request.Query.GetString("DepartmentName");

            var departments = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetDepartments(page, rows, ref total, orgId, departmentName: departmentName).ToArray();
            var list = _MakeDepartmentsTree(departments, parentId, orgId, not_id);




            return DefaultJson(new { total = total, rows = list });


            //return DefaultJson(list);
        }

        [HttpGet]
        public IActionResult GetDepartmentByID(int id)
        {
            var item = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetDepartmentByID(id);

            return DefaultJson(item);
        }

        [HttpPost]
        public IActionResult SaveDepartment([FromForm]Admin.CYT369.Com.DAL.Domains.Department department)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (department != null)
            {
                if (Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.SaveDepartment(department))
                {
                    resp.Result = true;
                    resp.Msg = System.Text.Json.JsonSerializer.Serialize(department);
                }
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }

        [HttpDelete]
        public IActionResult DeleteDepartment(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            try
            {
                if (Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.DeleteDepartment(id))
                {
                    resp.Result = true;
                }
                else
                {
                    resp.Msg = "删除失败";
                }

            }
            catch(Admin.CYT369.Com.Utility.Exceptions.DataExistException ex)
            {
                resp.Msg = ex.Message;
            }

            return DefaultJson(resp);
        }

    }
}